import {getObj, getContractApproveList} from '@/views/rent/api/rent-contract.js'
import {getObj as getClientManage} from '@/views/client-manage/api/clientlessee.js'
import pdf from 'vue-pdf'
//import uploadMedia from '@/components/upload-media'
// hxf-0731-引入vuex
import {mapState} from 'vuex'
//import typeSelect from "./type-select";
import contractChange from './components/contractChange.vue'
import contractDetails from './components/contractDetails.vue'
import contractRenew from './components/contractRenew.vue'
import contractApprove from './components/contractApprove.vue'
import contractBack from './components/contractBack.vue'
import uploadMedia from '@/components/upload-media'

export default {
    name: 'contract-view',
    components: {
        pdf,
        contractDetails,
        contractChange,
        contractRenew,
        contractBack,
        uploadMedia
    },
    data() {
        return {
            viewDialogVisible:false,

            activeName: 'first',
            formData: {},//合同总信息
            contractDetail: '',//合同明细
            rentContractProfee: '',//合同费用
            rentContractId: '',//合同id
            customerType: '',//承租方类型
            industryCategory: " ",//行业类别
            certificateType: "",//证件类型
            //文件上传
            uploadFileList: '',
            //合同预览
            pageNum: 1,
            category: 2,
            pageTotalNum: '',
            page: '',
            numPages:1,
            contractName: '',
            dialogContractVisible: false,
            pdfUrl: '',
            rentPriceRejustType: [],//调价方式
            rentCertificateType: [],//证件类型
            rentIndustryCategory: [],//行业类别
            rentSigningMethod: [],//签订方式
            rentFeeType: [],//收费方式
            //承租方内容展示
            //propForm: '',
            propForm:{
                clientLesseeType:'',
                clientLesseeName:'',
            },
            //合同内容展示
            //formData:'',
            propertyInformation: [],
            rentFeeInformation: [],
            contractFee: [],
            recordingLlist: [],
            //字典项匹配
            lessee: '',
            providerType: [],//供应商类型
            provider: '',
            commercialCertificateType: [],//工商证件类型
            commercialCertificate: '',
            personageCertificateType: [],//个人证件类型
            personageCertificate: '',
            subType: '',//合同细类
            type: '',//合同类型
            signType: '',//签订方式
            feeType: '',//计租方式
            approveType: '',//审批方式
            administrativeOffice: ''//管理所

        }

    },
    mounted:
        function () {
            //取出要查看的合同id
            //alert("11");

            //this.showFormData();

        },
    computed: mapState({
        //调价方式
        rent_price_rejust_type: state => {
            let data = state.dict.find(item => item.type === "rent_price_rejust_type") || {};
            return data.children || [];
        },
        //证件类型
        rent_certificate_type: state => {
            let data = state.dict.find(item => item.type === "rent_certificate_type") || {};
            return data.children || [];
        },
        //签订方式
        rent_signing_method: state => {
            let data = state.dict.find(item => item.type === "rent_signing_method") || {};
            return data.children || [];
        },
        //收费方式
        rent_fee_type: state => {
            let data = state.dict.find(item => item.type === "rent_fee_type") || {};
            return data.children || [];
        },
        //审批方式
        rent_contract_approve_type: state => {
            let data = state.dict.find(item => item.type === "rent_contract_approve_type") || {};
            return data.children || [];
        },
        //管理所
        administrative_office: state => {
            let data = state.dict.find(item => item.type === "administrative_office") || {};
            return data.children || [];
        },
        lessee_type: state => {
            let data = state.dict.find(item => item.type === "lessee_type") || {};
            return data.children || [];
        },
        provider_type: state => {
            let data = state.dict.find(item => item.type === "provider_type") || {};
            return data.children || [];
        },
        personage_certificate_type: state => {
            let data = state.dict.find(item => item.type === "personage_certificate_type") || {};
            return data.children || [];
        },
        commercial_certificate_type: state => {
            let data = state.dict.find(item => item.type === "commercial_certificate_type") || {};
            return data.children || [];
        },
        rent_contract_sub_type: state => {
            let data = state.dict.find(item => item.type === "rent_contract_sub_type") || {};
            return data.children || [];
        },
        rent_contract_type: state => {
            let data = state.dict.find(item => item.type === "rent_contract_type") || {};
            return data.children || [];
        },
    }),

    watch:{
        //$route:"changeView"
    },

    methods: {
        //获取审批列表
        async getContractApproveList(rentContractId) {
            let category = 1;
            const res = await getContractApproveList(rentContractId, category);
            if (res.data.code == 0) this.recordingLlist = res.data.data;
            else this.$alert("加载变更记录服务端服务错误" + res.code);
        },
        //查询表单数据
        async show(contractId) {

            //改为了弹出框模式
            this.viewDialogVisible=true;

            this.rentContractId=contractId;
            //if (this.$route.query.id != null) {
            //    this.rentContractId = this.$route.query.id;
            //}
            //alert(this.rentContractId);
            const response = await getObj(this.rentContractId,this.category);

            //alert(JSON.stringify(response));
            let formData = response.data.data;
            this.formData = formData;
            this.rentFeeInformation = formData.rentContractDetails
            this.propertyInformation = formData.rentContractRooms
            this.contractFee = formData.rentContractProfees

            //合同预览
            this.pdfUrl = formData.contractUrl;
            this.contractName = formData.contractName;

            //获得承租方信息
            if(this.formData.customerId) {
                const propForm = await getClientManage(this.formData.customerId,this.category)
                //if( propForm.data && propForm.data.data){
                this.propForm = propForm.data.data;
                //}
                this.showlessee();
            }
            //查看附件
            this.$refs.rentContractFileUpload.getFiles(this.formData.rentContractId);

            //5个子表
            //收费明细
            //alert();
            if(this.$refs.contractDetails && this.$refs.contractDetails.preViewFee && this.rentFeeInformation && this.rentFeeInformation.length>0) {
                this.$refs.contractDetails.preViewFee(contractId,this.formData);
            }
            if(this.$refs.contractChange && this.$refs.contractChange.loadHisList) {
                this.$refs.contractChange.loadHisList(contractId);
            }
            if(this.$refs.contractRenew && this.$refs.contractRenew.loadRenewList) {
                this.$refs.contractRenew.loadRenewList(contractId);
            }
            if(this.$refs.contractBack && this.$refs.contractBack.loadBackInfo) {
                this.$refs.contractBack.loadBackInfo(contractId);
            }
            this.getContractApproveList(contractId);
        },
        //展示证件类型
        showlessee() {
            //遍历字典，获得数据值对应中文描述
            for (var j = 0, len = this.lessee_type.length; j < len; j++) {
                if ( this.propForm!=null && this.lessee_type[j].value == this.propForm.clientLesseeType) {
                    this.lessee = this.lessee_type[j].label;
                }
            }
            for (var j = 0, len = this.provider_type.length; j < len; j++) {
                if (this.propForm!=null && this.provider_type[j].value == this.propForm.providerType) {
                    this.provider = this.provider_type[j].label;
                }
            }
            for (var j = 0, len = this.personage_certificate_type.length; j < len; j++) {
                if (this.propForm!=null && this.personage_certificate_type[j].value == this.propForm.certificateType) {
                    this.personageCertificate = this.personage_certificate_type[j].label;
                }
            }
            this.personageCertificate = '身份证'
            for (var j = 0, len = this.commercial_certificate_type.length; j < len; j++) {
                if (this.propForm!=null && this.commercial_certificate_type[j].value == this.propForm.certificateType) {
                    this.commercialCertificate = this.commercial_certificate_type[j].label;
                }
            }
            for (var j = 0, len = this.rent_signing_method.length; j < len; j++) {
                if (this.rent_signing_method[j].value == this.formData.signType) {
                    this.signType = this.rent_signing_method[j].label;
                }
            }
            for (var j = 0, len = this.rent_fee_type.length; j < len; j++) {
                if (this.rent_fee_type[j].value == this.formData.feeType) {
                    this.feeType = this.rent_fee_type[j].label;
                }
            }
            for (var j = 0, len = this.rent_contract_approve_type.length; j < len; j++) {
                if (this.rent_contract_approve_type[j].value == this.formData.approveType) {
                    this.approveType = this.rent_contract_approve_type[j].label;
                }
            }
            for (var j = 0, len = this.administrative_office.length; j < len; j++) {
                if (this.administrative_office[j].value == this.formData.administrativeOffice) {
                    this.administrativeOffice = this.administrative_office[j].label;
                }
            }
            for (var j = 0, len = this.rent_contract_sub_type.length; j < len; j++) {
                if (this.rent_contract_sub_type[j].value == this.formData.subType) {
                    this.subType = this.rent_contract_sub_type[j].label;
                }
            }
            for (var j = 0, len = this.rent_contract_type.length; j < len; j++) {
                if (this.rent_contract_type[j].value == this.formData.type) {
                    this.type = this.rent_contract_type[j].label;
                }
            }

        },
        //合同预览
        //上一页
        prePage() {
            var p = this.pageNum
            p = p > 1 ? p - 1 : this.pageTotalNum
            this.pageNum = p
        },
        //下一页
        nextPage() {
            var p = this.pageNum
            p = p < this.pageTotalNum ? p + 1 : 1
            this.pageNum = p
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => {
                    done();
                })
                .catch(_ => {
                });
        },
        close(){
            //alert("aa");
            this.viewDialogVisible=false;
        }

    }
}


